package com.ran.asynctaskdemo;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends BaseActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    private TextView textView;
    private ProgressBar progressBar;

    private ProgressDialog pd1 = null;
    private ProgressDialog pd2 = null;
    private final static int MAXVALUE = 100;
    private int progressStart = 0;
    private int add = 0;
    private Context mContext = null;

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 123) {
                pd2.setProgress(progressStart);
            }
            if (progressStart >= MAXVALUE) {
                pd2.dismiss();
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        initViews();
    }

    @Override
    protected void initViews() {
        textView = (TextView) findViewById(R.id.textView);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        Paint paint = new Paint();
    }

    public void updateClick(View view) {
        MyAsyncTask myTask = new MyAsyncTask(textView, progressBar);
        myTask.execute(1000);
    }

    public void dialogClick1(View view) {
        ProgressDialog.show(MainActivity.this, "资源加载中", "资源加载中,请稍后...", false, true);
    }

    public void dialogClick2(View view) {
        pd1 = new ProgressDialog(mContext);
        //依次设置标题,内容,是否用取消按钮关闭,是否显示进度
        pd1.setTitle("软件更新中");
        pd1.setMessage("软件正在更新中,请稍后...");
        pd1.setCancelable(true);
        //这里是设置进度条的风格,HORIZONTAL是水平进度条,SPINNER是圆形进度条
        pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd1.setIndeterminate(true);
        //调用show()方法将ProgressDialog显示出来
        pd1.show();
    }

    public void dialogClick3(View view) {
        //初始化属性
        progressStart = 0;
        add = 0;
        //依次设置一些属性
        pd2 = new ProgressDialog(MainActivity.this);
        pd2.setMax(MAXVALUE);
        pd2.setTitle("文件读取中");
        pd2.setMessage("文件加载中,请稍后...");
        //这里设置为不可以通过按取消按钮关闭进度条
        pd2.setCancelable(false);
        pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //这里设置的是是否显示进度,设为false才是显示的哦！
        pd2.setIndeterminate(false);
        pd2.show();
        //这里的话新建一个线程,重写run()方法,
        new Thread() {
            public void run() {
                while (progressStart < MAXVALUE) {
                    //这里的算法是决定进度条变化的,可以按需要写
                    progressStart = 2 * usetime();
                    //把信息码发送给handle让更新界面
                    handler.sendEmptyMessage(123);
                }
            }
        }.start();
    }

    //这里设置一个耗时的方法:
    private int usetime() {
        add++;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return add;
    }
}
